//
//  ModifyPhoneViewController.m
//  parent
//
//  Created by 闫珍 on 15/6/5.
//  Copyright (c) 2015年 ci123. All rights reserved.
//

#import "ModifyPhoneViewController.h"
#import "PopUIManager.h"
#import "AFAppClient.h"
#import "PhoneNumberChecker.h"
#import "UserDao.h"
#import "JsonUtil.h"
#import "NetWorkManager.h"
#import "TeacherViewController.h"

@interface ModifyPhoneViewController ()
@property (weak, nonatomic) IBOutlet UILabel *tipsLabel;
@property (weak, nonatomic) IBOutlet UILabel *phoneLabel;
@property (weak, nonatomic) IBOutlet UITextField *codeTxt;
@property (weak, nonatomic) IBOutlet UITextField *pwdTxt;
@property (weak, nonatomic) IBOutlet UIButton *changeBtn;

@end

@implementation ModifyPhoneViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self customView];
}

-(void)customView
{
    if ([self respondsToSelector:@selector(edgesForExtendedLayout)])
        self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"修改手机号";
    self.view.backgroundColor = UIColorMake(241, 241, 241);
    _phoneLabel.textColor = UIColorMake(101, 101, 101);
    _tipsLabel.textColor = UIColorMake(101, 101, 101);
    _phoneLabel.text = _phone;
    
    _codeTxt.layer.borderColor = UIColorMake(183, 183, 183).CGColor;
    _pwdTxt.layer.borderColor = UIColorMake(183, 183, 183).CGColor;
    
    _changeBtn.backgroundColor=UIColorMake(252, 74, 74);

    _changeBtn.layer.cornerRadius = 5.0;

    NSMutableAttributedString *text =
    [[NSMutableAttributedString alloc]
     initWithString: @"我们已经发送验证码短信到这个号码"];
    
    [text addAttribute:NSForegroundColorAttributeName
                 value:UIColorMake(241, 76, 92)
                 range:NSMakeRange(6, 5)];
    [_tipsLabel setAttributedText: text];
    
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 20 , 44, 44)];
    button.showsTouchWhenHighlighted = YES;
    button.backgroundColor = [UIColor clearColor];
    button.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 30);
    [button setImage:[UIImage imageNamed:@"nav_btn_back"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    
    UIBarButtonItem *backButton = [[UIBarButtonItem alloc]
                                   initWithTitle:@""
                                   style:UIBarButtonItemStylePlain
                                   target:nil
                                   action:nil];
    self.navigationItem.backBarButtonItem=backButton;
    
}
-(void)backButton:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)doChange:(id)sender {
    NSString *code = _codeTxt.text;
    NSString *pwd = _pwdTxt.text;
    
    if([code isEqualToString:@""]||code.length==0){
        [[PopUIManager getInstanse]popAlertView:nil title:@"提示" message:@"请输入验证码" cancelButtonTitle:@"确定"];
        return;
    }
    
    if([pwd isEqualToString:@""]){
        [[PopUIManager getInstanse]popAlertView:nil title:@"提示" message:@"请输入密码" cancelButtonTitle:@"确定"];
        return;
    }
    
    NSDictionary *param = @{@"uid":@([[UserDao getInstance]getUserId]),
        @"utype":@(1),
        @"token":[[UserDao getInstance]getAccessToken],
        @"authcode":code,
        @"pwd":pwd,
        @"phone":_phone
        };
    NSString *dataString = [JsonUtil convertDictionaryToJson:param];
    
    PopUIManager *popUIMgr = [PopUIManager getInstanse];
    [popUIMgr showWaitingUI:self.view];
    
    [[NetWorkManager getInstanse] requestURLAsynMessage:USER_MODIFY_PHONE data:dataString FinishBlock:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        if (connectionError) { //连接失败
            NSLog(@"Http Error: %@   %ld", connectionError.localizedDescription,(long)connectionError.code);
            NSString *error_content = [NSString stringWithFormat:@"连接错误:%ld",(long)connectionError.code];
            [popUIMgr popAlertView:nil title:@"提示" message:error_content cancelButtonTitle:@"确定"];
        }else{//连接成功，判断是否有数据返回
            if (data) {
                NSDictionary *dict = [JsonUtil convertDataToDictionary:data];
                NSLog(@"数据字典: %@", dict);
                
                if ([[dict objectForKey:@"ret"] integerValue]== 1) {
                    //登录失败，弹窗提示
                    NSString *ret_msg;
                    switch ([[dict objectForKey:@"retCode"] integerValue]) {
                        case 10005:{
                            ret_msg= @"更改失败";
                            break;
                        }case 1:{
                            ret_msg= @"Success";
                            break;
                        }case 10003:{
                            ret_msg= @"验证码错误";
                            break;
                        }case 10022:{
                            ret_msg= @"密码错误";
                            break;
                        }default:{
                            ret_msg= @"未知错误";
                            break;
                        }
                    }
                    if (![ret_msg isEqual:@"Success"]) {
                        [popUIMgr popAlertView:nil title:@"提示" message:ret_msg cancelButtonTitle:@"确定"];
                        [popUIMgr removeWaitingUI];
                        return;
                    }else{
                       
                        NSArray *views = self.navigationController.viewControllers;
                        for (UIViewController* view in views) {
                            if([view isKindOfClass:[TeacherViewController class]]){
                                [self.navigationController popToViewController:view animated:YES];
                                return;
                            }
                        }
                    }
                }else{
                    [popUIMgr popAlertView:nil title:@"提示" message:@"数据解析错误" cancelButtonTitle:@"确定"];
                }
            }else{
                [popUIMgr popAlertView:nil title:@"提示" message:@"服务器没有返回数据" cancelButtonTitle:@"确定"];
            }
        }
        [popUIMgr removeWaitingUI];
    }];


}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
